package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type HisDoctorRepository interface {
	Create(doctor *model.HisDoctor) error
	GetByID(id uint) (*model.HisDoctor, error)
	GetByDoctorCode(doctorCode string) (*model.HisDoctor, error)
	Update(doctor *model.HisDoctor) error
	Delete(id uint) error
	ListByHospital(hospitalCode string) ([]model.HisDoctor, error)
	ListByDepartment(deptCode string) ([]model.HisDoctor, error)
}
type gormHisDoctorRepository struct {
	db *gorm.DB
}

func NewGormHisDoctorRepository(db *gorm.DB) HisDoctorRepository {
	return &gormHisDoctorRepository{db: db}
}

func (r *gormHisDoctorRepository) Create(doctor *model.HisDoctor) error {
	return r.db.Create(doctor).Error
}

func (r *gormHisDoctorRepository) GetByID(id uint) (*model.HisDoctor, error) {
	var doctor model.HisDoctor
	err := r.db.First(&doctor, id).Error
	return &doctor, err
}

func (r *gormHisDoctorRepository) GetByDoctorCode(doctorCode string) (*model.HisDoctor, error) {
	var doctor model.HisDoctor
	err := r.db.Where("coding = ?", doctorCode).First(&doctor).Error
	return &doctor, err
}

func (r *gormHisDoctorRepository) Update(doctor *model.HisDoctor) error {
	return r.db.Save(doctor).Error
}

func (r *gormHisDoctorRepository) Delete(id uint) error {
	return r.db.Delete(&model.HisDoctor{}, id).Error
}

func (r *gormHisDoctorRepository) ListByHospital(hospitalCode string) ([]model.HisDoctor, error) {
	var doctors []model.HisDoctor
	err := r.db.Where("hospital_code = ?", hospitalCode).Find(&doctors).Error
	return doctors, err
}

func (r *gormHisDoctorRepository) ListByDepartment(deptCode string) ([]model.HisDoctor, error) {
	var doctors []model.HisDoctor
	err := r.db.Where("department = ?", deptCode).Find(&doctors).Error
	return doctors, err
}
